﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;

namespace BAL
{
    public static class ScoreGrade
    {
        public static string GetGrade(decimal score)
        {
            string grade = string.Empty;
            if (score >= 85 && score <= 100)
            {
                grade = "<span class='face_a' title='优'>优</span>【优】";
            }
            else if (score >= 70 && score < 85)
            {
                grade = "<span class='face_b' title='良'>良</span>【良】";
            }
            else if (score >= 60 && score < 70)
            {
                grade = "<span class='face_c' title='中'>中</span>【中】";
            }
            else if (score >= 0 && score < 60)
            {
                grade = "<span class='face_d' title='差'>差</span>【差】";
            }
            else
            {
                throw new Exception("成绩不在范围之内");
            }
            return grade;
        }
    }

    public class ScoreUnit
    {
        public string Type { get; set; }

        public decimal AverageScore { get; set; }

        public decimal MaxScore { get; set; }

        public int StudentNum { get; set; }
    }

    public class StudentAnalyPdfReport
    {
        private ExamRoomInfo _examroomInfo;
        private StudentInfo _studentInfo;
        private StudentScoreExtendInfo _studentScoreExtendInfo;
        private ScoreUnit _personScoreUnit = new ScoreUnit();
        private ScoreUnit _classScoreUnit = new ScoreUnit();
        private ScoreUnit _gradeScoreUnit = new ScoreUnit();
        private ScoreUnit _areaScoreUnit = new ScoreUnit();

        public StudentAnalyPdfReport(int examId, int studentId)
        {
            _examroomInfo = ExamRoom.GetSingleInfo(examId);
            _studentInfo = Student.GetSingleInfo(studentId);
            if (_examroomInfo == null)
            {
                throw new Exception("考试模型不存在，请联系管理员");
            }
            if (_studentInfo == null)
            {
                throw new Exception("学生模型不存在，请联系管理员");
            }
        }

        public void AnalyAndGeneratePdf()
        {

        }

        private void PrepareData()
        {
            //comment = _examroomInfo.Comment;
            IList<StudentScoreExtendInfo> infos = StudentScoreExtend.GetList(_examroomInfo.Id, 0, 0, 0, 0, 0, 0, 0);
            StudentScoreExtendInfo myInfo = infos.Single(t => t.StudentId == _studentInfo.Id);

            _personScoreUnit = new ScoreUnit()
            {
                AverageScore = myInfo.CountScore
            };

            decimal classcount = infos.Where(t => t.ClassId == myInfo.ClassId).Sum(t => t.CountScore);
            StudentScoreExtendInfo classinfo = infos.Where(t => t.ClassId == myInfo.ClassId).OrderByDescending(t => t.CountScore).First();
            int classrencount = infos.Where(t => t.ClassId == myInfo.ClassId).Count();
            _classScoreUnit = new ScoreUnit()
            {
                AverageScore = Math.Round((classcount / classrencount), 1),
                StudentNum = classrencount,
                MaxScore = classinfo.CountScore
            };

            decimal gradecount = infos.Where(t => t.GradeId == myInfo.GradeId).Sum(t => t.CountScore);
            StudentScoreExtendInfo gradecountinfo = infos.Where(t => t.GradeId == myInfo.GradeId).OrderByDescending(t => t.CountScore).First();
            int graderencount = infos.Where(t => t.GradeId == myInfo.GradeId).Count();
            _gradeScoreUnit = new ScoreUnit()
            {
                AverageScore = Math.Round((gradecount / graderencount), 1),
                StudentNum = graderencount,
                MaxScore = gradecountinfo.CountScore
            };

            decimal areacount = infos.Where(t => t.AreaId == myInfo.AreaId).Sum(t => t.CountScore);
            StudentScoreExtendInfo areainfo = infos.Where(t => t.AreaId == myInfo.AreaId).OrderByDescending(t => t.CountScore).First();
            int arearencount = infos.Where(t => t.AreaId == myInfo.AreaId).Count();
            _areaScoreUnit = new ScoreUnit()
            {
                AverageScore = Math.Round((areacount / arearencount), 1),
                StudentNum = arearencount,
                MaxScore = areainfo.CountScore
            };

            string grade = ScoreGrade.GetGrade(myInfo.CountScore);



        }


    }
}
